<!--
 * @Description:总账目详情
-->
<template>
  <div class="registered-container unilatAccount appMainWarp">

    <el-form :inline="true" :model="searchKey" class="demo-form-inline">

      <el-form-item>
        <el-button :type="0 == quaflat ? 'primary':''" @click="getlisttable(0)">昨天</el-button>
        <el-button :type="1 == quaflat ? 'primary':''" @click="getlisttable(1)">近七天 </el-button>
        <el-button :type="2 == quaflat ? 'primary':''" @click="getlisttable(2)">近1个月</el-button>
      </el-form-item>

      <el-form-item label="交易时间:">
        <el-date-picker
          v-model="searchKey.time"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          :picker-options="pickerOptions"
        />
      </el-form-item>
      <el-form-item label="院区:">
        <el-select v-model="searchKey.hospital_area_no" placeholder="请选择">
          <el-option
            v-for="item in searchKey.Hospital"
            :key="item.area_code"
            :label="item.area_name"
            :value="item.area_code"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="订单号:">
        <el-input v-model="searchKey.order_no" placeholder="请输入订单号" @change="suborder_no" />
      </el-form-item>

      <el-form-item label="交易类型:">
        <el-select v-model="searchKey.trans_type" placeholder="请选择">
          <el-option
            v-for="item in searchKey.compensationWayList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="选择支付场景:">
        <el-select v-model="searchKey.system_no" multiple placeholder="请选择支付场景" clearable>
          <el-option
            v-for="item in searchKey.System"
            :key="item.pay_scene_no"
            :label="item.pay_scene_name"
            :value="item.pay_scene_no"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="支付方式:">
        <el-select v-model="searchKey.pay_style" placeholder="请选择">
          <el-option
            v-for="item in searchKey.PayStyle"
            :key="item.pay_no"
            :label="item.name"
            :value="item.pay_no"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="对账业务:">
        <el-select v-model="searchKey.reconciliation" placeholder="请选择" @change="onLoad">
          <el-option
            v-for="item in searchKey.reconciliationList"
            :key="item.code"
            :label="item.name"
            :value="item.code"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="业务类型:">
        <el-cascader
          v-model="searchKey.bill_type"
          :options="searchKey.BillType"
          :props="props"
          change-on-select
          clearable
        />
      </el-form-item>

      <!-- <el-form-item label="选择有数据系统:">
        <el-select v-model="searchKey.is_data" placeholder="请选择">
          <el-option
            v-for="item in searchKey.is_dataList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item> -->

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="onLoad">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="success" icon="el-icon-download" @click="onExport">导出明细</el-button>
      </el-form-item>

      <!-- <div v-if="conf_key== 'BANK_DATA'" class="tips">
        {{ conf_desc }}
      </div> -->
    </el-form>

    <template>
      <el-table
        ref="table"
        v-loading="page.tableLoading"
        v-el-height-adaptive-table="{bottomOffset: 90}"
        max-height="500px"
        center
        class="table"
        :data="searchDate"
        stripe
        border
      >
        <!-- <el-table-column type="index" width="90" label="序号" /> -->
        <el-table-column label="交易时间" align="center">
          <template slot-scope="scope">
            <div :class="[scope.row.color ? scope.row.color:'red']">
              {{ scope.row.trans_time||'-' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="支付场景" align="center">
          <template slot-scope="scope">
            <div :class="[scope.row.color ? scope.row.color:'red']">
              {{ scope.row.system_name||'-' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="支付场景订单号" align="center">
          <template slot-scope="scope">
            <div :class="[scope.row.color ? scope.row.color:'red']">
              {{ scope.row.self_order_no||'-' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="HIS系统订单号" align="center">
          <template slot-scope="scope">
            <div :class="[scope.row.color ? scope.row.color:'red']">
              {{ scope.row.his_order_no||'-' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="交易流水号" align="center">
          <template slot-scope="scope">
            <div :class="[scope.row.color ? scope.row.color:'red']">
              {{ scope.row.his_order_no||'-' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="支付场景金额" align="center">
          <template slot-scope="scope">
            <div :class="[scope.row.color ? scope.row.color:'red']">
              {{ scope.row.self_pay_fee||'-' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="HIS系统金额" align="center">
          <template slot-scope="scope">
            <div :class="[scope.row.color ? scope.row.color:'red']">
              {{ scope.row.his_pay_fee||'-' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="支付系统金额" align="center">
          <template slot-scope="scope">
            <div :class="[scope.row.color ? scope.row.color:'red']">
              {{ scope.row.three_pay_fee||'-' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="订单状态" align="center">
          <template slot-scope="scope">
            <el-tag :class="[scope.row.color ? scope.row.color:'red']" type="danger">订单异常</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="异常说明" align="center">
          <template slot-scope="scope">
            <div :class="[scope.row.color ? scope.row.color:'red']">
              {{ scope.row.desc||'-' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="交易类型" align="center">
          <template slot-scope="scope">
            <div :class="[scope.row.color ? scope.row.color:'red']">
              {{ scope.row.trans_type_name||'-' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="交易院区" align="center">
          <template slot-scope="scope">
            <div :class="[scope.row.color ? scope.row.color:'red']">
              {{ scope.row.area_name||'-' }}
            </div>
          </template>
        </el-table-column>

        <el-table-column label="业务类型" align="center">
          <template slot-scope="scope">
            <div :class="[scope.row.color ? scope.row.color:'red']">
              {{ scope.row.bill_type_name||'-' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="是否已处理" align="center">
          <template slot-scope="scope">
            <div>
              {{ scope.row.handle||'-' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150" align="center">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="jumpClick(scope.row)">查看详情</el-button>
            <el-button v-if="scope.row.trans_type===2" style="margin:5px auto;" size="mini" type="success" @click="expandChange(scope.row,scope.$index)">{{ scope.row.rowFlag?'收起正交易信息':'查看正交易信息' }}</el-button>
          </template>
        </el-table-column>
        <el-table-column type="expand" width="1">
          <div slot-scope="scope" class="boder">
            <el-form v-if="scope.row.item" label-width="30px" label-position="left" style="text-align:left">
              <div class="applicant">
                <div class="prominent" />
                <span>正交易订单信息</span>
              </div>
              <div class="orderItem">
                <span>交易院区：</span>
                {{ scope.row.item.area_name }}
              </div>
              <div class="orderItem">
                <span>交易时间：</span>
                {{ scope.row.item.trans_time }}
              </div>
              <div class="orderItem">
                <span>支付场景：</span>
                {{ scope.row.item.pay_scene }}
              </div>
              <div class="orderItem">
                <span>支付场景订单号：</span>
                {{ scope.row.item.out_trade_no }}
              </div>
              <div class="orderItem">
                <span>交易金额：</span>
                {{ scope.row.item.pay_fee }}
              </div>
              <div class="orderItem">
                <span>交易类型：</span>
                {{ scope.row.item.trans_type }}
              </div>
              <div class="orderItem">
                <span>业务类型：</span>
                {{ scope.row.item.bill_type_no }}
              </div>
              <div class="orderItem">
                <span>支付方式：</span>
                {{ scope.row.item.pay_style_name }}
              </div>
              <div class="orderItem">
                <span>交易流水号：</span>
                {{ scope.row.item.trade_no }}
              </div>
              <div class="orderItem">
                <span>订单状态：</span>
                {{ scope.row.item.status }}
              </div>
            </el-form>
          </div>
          <!-- <template slot-scope="scope">
            <el-tabs type="border-card">
              <el-tab-pane label="退款详情">
                <el-form label-width="100px" label-position="left">
                  <el-form-item label="退款信息:" label-width="100px">
                    <div class="details">
                      <div class="orderItem">
                        <span>退款金额:</span>
                      </div>
                      <div class="orderItem">
                        <span>退款状态:</span>
                      </div>
                      <div class="orderItem">
                        <span>退款成功时间:</span>
                      </div>
                      <div class="orderItem">
                        <span>退款账户:</span>
                      </div>
                    </div>
                  </el-form-item>
                </el-form>
              </el-tab-pane>
            </el-tabs>
          </template> -->
        </el-table-column>
      </el-table>
    </template>
    <pagination
      :total="page.totalRow"
      :page.sync="page.currentPage"
      :limit.sync="page.pageSize"
      @pagination="onSearch"
    />

  </div>
</template>

<script>
import elHeightAdaptiveTable from '@/directive/el-table'
import { getConfigType } from '@/api/transactRecords/index'
import { singleBillDetail, paytradeinfo } from '@/api/detailed/index'
import { getCompareType } from '@/utils/auth'
import Pagination from '@/components/Pagination' // 分页
export default {
  name: 'DayspanAccount',
  components: {
    Pagination
  },
  directives: { elHeightAdaptiveTable },

  data() {
    return {
      quaflat: 0, // 按钮默认
      date: 'yesterday', // 昨天
      searchKey: {
        order_no: '', // 订单号
        time: '', // 交易日期
        hospital_area_no: '', // 选择院区
        Hospital: [],
        trans_type: '', // 交易类型
        compensationWayList: [
          { id: '', name: '全部' },
          { id: 1, name: '正交易' },
          { id: 2, name: '负交易' }
        ],
        system_no: '', // 选择平台
        System: [],
        pay_style: '', // 支付方式
        PayStyle: [],
        reconciliation: '', // 对账业务
        reconciliationList: [],
        bill_type: [], // 业务类型
        BillType: []
      },
      props: {
        label: 'name',
        value: 'bill_no',
        children: 'child'
      },

      searchDate: [],
      BankData: {}, // 银行明细配置
      conf_key: '',
      conf_desc: '',

      page: {
        tableLoading: false,
        currentPage: 1, // 当前页
        pageSize: 20, // 默认条数
        totalRow: 0, // 总条数
        totalPage: 0 // 总页数
      },
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now() || time.getTime() === Date.now() || time.getTime() < new Date().setFullYear((new Date().getFullYear() - 1))
        }
      },
      compare: '',
      system: '' // 平台
    }
  },

  created() {
    this.searchKey.reconciliation = getCompareType()
    this.searchKey.time = this.utils.Foryest()
  },
  mounted() {
    this.getConfig()
    this.onLoad()
  },
  methods: {
    expandChange(row, index) {
      var that = this
      row.rowFlag = !row.rowFlag
      if (row.rowFlag) {
        var params = {}
        params.outtradeno = row.self_order_no
        params.transtype = 1
        that.page.tableLoading = true
        paytradeinfo(params).then((res) => {
          console.log(res)
          this.searchDate[index].item = res.data
          that.page.tableLoading = false
          that.$nextTick(() => {
            that.$refs.table.toggleRowExpansion(row, true)// 加载完数据展开行
          })
        }).catch(() => {
          that.page.tableLoading = false
        })
      } else {
        this.$refs.table.toggleRowExpansion(row, false)// 关闭行
      }
    },
    // 系统配置搜索
    getConfig() {
      var that = this
      var params = {}
      getConfigType(params).then(res => {
        that.searchKey.Hospital = res.data.hospital
        that.searchKey.reconciliationList = res.data.compare
        that.searchKey.PayStyle = res.data.payment // 支付方式
        that.searchKey.System = res.data.scene// 场景
        that.searchKey.BillType = res.data.bill// 业务类型
      })
        .catch(() => {
        })
    },
    // 递归判断列表，把最后的children的内容不显示
    getTreeData(data) {
      for (var i = 0; i < data.length; i++) {
        if (data[i].data.length < 1) {
          // children若为空数组，则将children设为undefined
          data[i].data = undefined
        } else {
          // children若不为空数组，则继续 递归调用 本方法
          this.getTreeData(data[i].data)
        }
      }
      return data
    },
    onLoad() {
      this.page.currentPage = 1
      this.onSearch()
    },
    onSearch() {
      var params = {}
      var that = this
      params.page = that.page.currentPage // 当前页数
      params.limit = that.page.pageSize // 每页展示条数
      params.datetype = that.date
      params.outlier = 4
      if (that.searchKey.time && that.searchKey.time.length > 0) {
        params.starttime = that.searchKey.time[0]
        params.endtime = that.searchKey.time[1]
      } else {
        params.starttime = ''
        params.endtime = ''
      }
      params.orderno = that.searchKey.order_no
      params.areacode = that.searchKey.hospital_area_no
      params.transtype = that.searchKey.trans_type
      params.sceneno = that.searchKey.system_no.toString()
      params.paystyle = that.searchKey.pay_style
      params.billtype = that.searchKey.bill_type.length > 1 ? that.searchKey.bill_type[1] : that.searchKey.bill_type[0] || ''
      params.comparetype = that.searchKey.reconciliation.toString()
      that.page.tableLoading = true
      singleBillDetail(params)
        .then(res => {
          that.page.tableLoading = false
          that.searchDate = res.data.result
          that.page.totalPage = res.data.pages
          that.page.totalRow = parseInt(res.data.count)
        })
        .catch(() => {
          that.page.tableLoading = true
        })
    },
    jumpClick(item) {
      this.$router.push({
        path: '/generaLedger/totalRevenue/revenueDetail/index',
        query: { id: item.id, type: this.searchKey.reconciliation, status: item.status, transtype: item.trans_type, time: item.trans_time.substring(0, 10) }
      })
    },
    // 导出
    onExport() {
      var params = {}
      var that = this
      params.page = that.page.currentPage // 当前页数
      params.limit = that.page.pageSize // 每页展示条数
      params.datetype = that.date
      params.outlier = 4
      if (that.searchKey.time && that.searchKey.time.length > 0) {
        params.starttime = that.searchKey.time[0]
        params.endtime = that.searchKey.time[1]
      } else {
        params.starttime = ''
        params.endtime = ''
      }
      params.orderno = that.searchKey.order_no
      params.areacode = that.searchKey.hospital_area_no
      params.transtype = that.searchKey.trans_type
      params.sceneno = that.searchKey.system_no.toString()
      params.paystyle = that.searchKey.pay_style
      params.billtype = that.searchKey.bill_type.length > 1 ? that.searchKey.bill_type[1] : that.searchKey.bill_type[0] || ''
      params.comparetype = that.searchKey.reconciliation.toString()
      var viewUrl = `/cms/singlebill?&page=${params.page}&limit=${params.limit}&datetype=${
        params.datetype
      }&orderno=${params.orderno}&transtype=${params.transtype}&outlier=${params.outlier}&areacode=${
        params.areacode
      }&starttime=${
        params.starttime
      }&endtime=${params.endtime}&sceneno=${params.sceneno}&paystyle=${params.paystyle}&billtype=${params.billtype}&comparetype=${params.comparetype}&export=${1}`
      window.location.href = process.env.VUE_APP_BASE_API + viewUrl
    },
    // 验证订单号
    suborder_no() {
      const id = this.searchKey.order_no
      const reg = /^[0-9a-zA-Z]+$/
      if (!reg.test(id)) {
        this.$message({
          type: 'warning',
          message: '请输入正确的订单号'
        })
        this.searchKey.order_no = ''
        return false
      }
    },
    getlisttable(flat) {
      this.quaflat = flat
      if (this.quaflat === 0) {
        this.date = 'yesterday'
        this.searchKey.time = this.utils.Foryest()
        this.onLoad()
      } else if (this.quaflat === 1) {
        this.date = 'sevenday'
        this.searchKey.time = this.utils.Forseven()
        this.onLoad()
      } else {
        this.date = 'month'
        this.searchKey.time = this.utils.Formonth()
        this.onLoad()
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.unilatAccount {
  position: relative;
  width: 100%;
  .demo-form-inline {
    .el-button--primary {
      background-color: #579ef8 !important;
      color: #fff !important;
    }
   .el-button {
      background-color: #e8f4ff;
      color: #579ef8;
      border: 1px solid rgba(87, 158, 248, 1);
    }
  }
  .red {
    color: red !important;
  }
  .blue {
    color: blue !important;
  }
  .yellow {
    color:yellow !important;
  }
  .isDot {
    vertical-align: -webkit-baseline-middle;
    color: #1890FF;
  }
  .viewdetails{
    color: #1890FF;
    cursor: pointer;
  }
  .tips{
    color: red;
    font-size: 18px;
    padding: 0 29px 15px;
  }
  .applicant {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    padding: 10px 0;
    .prominent {
      width: 4px;
      height: 24px;
      background: rgba(87, 158, 248, 1);
      border-radius: 2px;
    }
    span {
      padding-left: 19px;
      font-size: 20px;
      color: #232d50;
      font-weight: bold;
    }
  }
  .orderItem {
    display: inline-block;
    vertical-align: top;
    width: 380px;
    span {
      font-size: 15px;
      color: #333;
      font-weight: 400;
    }
  }
}
</style>

